//翡翠筛选
var PlaceOrder=false;//代下单
(function(){
	var emeraldsTerm={
		init:function(){
			args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//2是现货					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;
			} 
			this.commonParamers();
			this.bindEvent();
		},
		bindEvent:function(){
			var self=this;
			if(TEAM==2){
				$(".team_params").show();
				$(".normal_params").hide();
			}else{
				$(".team_params").hide();
				$(".normal_params").show();
			}
			//切换类型显示类型细分
			$("#emerald_type").on("click","span",function(){
				$(this).toggleClass("active");											
			});
			//点击选中                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			$("#term_page").on("click",".term span",function(){
				$(this).toggleClass("active");
				//切换类型显示类型细分
				if($(this).parent().hasClass("type")){
					var length=$("#Type span.active").length;					
					if(length==1){
						var type=$("#Type span.active").attr("data-value");
						self.changType(type);
					}else{
						$("#Type2").remove();
					}
				}
			});
			//点击搜索
			$("#filter_product_btn").on("click",function(){
				self.getQueryParameters();
				
			});
			//筛选重置
            $(".reset_term_btn").on("click", function () {
                 $(".term span").removeClass("active");//清除其他条件选中的样式
               $(".term-box input").val("");//清除input框里的值
            });
		},
		//供应商输入提示
		loadSupplier:function(){
			  $.ajax({
		         url: "/cg-api/suppliers-dc/?is_classify=1",//提交地址
		         type: "GET",//提交方式
		         success: function (result) {
		         	
		             if (result.result == true) {
						var availableTags = [];
		             	 for (var i = 0; i < result.rows.length; i++) {
		             	 	var row=result.rows[i];
		             	 	if(row.classify.emeralds==true){
		             	 		var supplier_name=row.short_name?(row.short_name):(row.supplier);
		             	 		availableTags.push(supplier_name);
		             	 	}		             	 	
		             	 }
						$("#Supplier").autocomplete({
						 	 source: availableTags
						});
		             }		            		
		         }
		     });
		},
		//公共的筛选参数
		commonParamers:function(){
			//类型
			var Type = '<h4>'+_("类型")+'</h4>'
			for (var i = 0; i < emerald[1].Type.length; i++) {
					Type += '<span data-value="'+emerald[1].Type[i][0]+'">'+ _(emerald[1].Type[i][1]) +'</span>'
			}
			$("#Type").html(Type);
			//种地
			var Texture='<h4>'+_("种地")+'</h4>'
			for (var i = 0; i < emerald[1].Texture.length; i++) {
				Texture += '<span data-value="'+emerald[1].Texture[i][0]+'">'+ _(emerald[1].Texture[i][1]) +'</span>'
			}
			$("#Texture").html(Texture);
		},
		// 切换类型显示类型细分
		changType:function(type){
			$("#Type2").remove();	
			if(type){
				//类型细分
				var Type2 = '<div data-name="Type2" class="term" id="Type2"><h4>'+_("类型细分")+'</h4>';
				for (var i = 0; i < emerald_type_category[type].Type2.length; i++) {
						Type2 += '<span data-value="'+emerald_type_category[type].Type2[i][0]+'">'+ _(emerald_type_category[type].Type2[i][1]) +'</span>'
				}
				Type2+='</div>'				
				$("#Type").after(Type2);//颜色			
			}
			
		},
		//获取参数
        getQueryParameters: function () {
           var params = {};
            params.limit = "500";
            params.useDict = true;
            isSearching = true;
            var pageSize = 1;//500
            //查询开始时间
            var dtSearchStart = new Date().getTime();
            //循环找出筛选框里选中的参数
            $("#emerald_term_box .term").each(function (i, item) {
            	var field = $(item).attr("data-name");
		        var dataType = $(item).attr("data-type");
		        if (field) {
		            var values = [];
		            $(item).find("span.active").each(function (j, p) {
		                var value = $(p).attr("data-value");
		                value = value ? value : $(p).text();
//		                if(field!="Price"){
//		                	value = dataType == 'range' ? value.split('-') : value;
//		                }		                
		                values.push(value);
		            });
		
		            if (values.length > 0) {
		                if (dataType == 'range') {
		                    if (!params.between) {
		                        params.between = {};
		                    }
		                   if(field == "Price") { //总价
								if(!params.between.Price)
									params.between.Price = []; //初始化
								if(values.length == 1) {
									params.between.Price.push(values[0].split("-")[0]);
									params.between.Price.push(values[0].split("-")[1]);
								} else if(values.length > 1) {
									var min = values[0];
									var max = values[values.length - 1];
									params.between.Price.push(min.split("-")[0]);
									params.between.Price.push(max.split("-")[1]);
								}
							}else { //重量
								if(!params.between.MainWeight)
									params.between.MainWeight = []; //初始化    
								var min = values[0];
								var max = values[values.length - 1];
								params.between.MainWeight.push(min.split("-")[0]);
								params.between.MainWeight.push(max.split("-")[1]);
							}
		                    
		                } else {
		                    params[field] = values;
		                }
		            }
		        }                    
           });          
		    var supplier = $("#Supplier").val();
		    if (supplier!="") {
		        params.shortName = supplier;
		    }
		    var OriginSN=$("#OriginSN").val();
		    if(OriginSN!=""){
		    	params.OriginSN=OriginSN;
		    }
		    params.Category=1;//暂时只有翡翠
			
			if(TEAM==0){
				var url_href="/Customer/Emerald?team=0";//客户端的统包页面
			}else if(TEAM==1){
				var url_href="/Team/Emerald?team=1";//团队端的统包页面
			}else if(TEAM==2){
				var url_href="/Team/MyEmerald?team=2";//团队端的现货统包页面
			}else if(TEAM==3){
				var url_href="/Team/Emerald?team=3&cid="+Customer_id;//团队端的代下单
			}
			var action=url_href;
		  	form = $("<form method='post' id='params_form' action='"+action+"'>"+
		   	"<input type='text' value='"+JSON.stringify(params)+"' name='params'></form>")
			$("#form_box").html(form);
			$("#params_form").submit();
        },
	}
emeraldsTerm.init();
})();
